package es.capgemini.extjsplugin.wizard;

import java.lang.reflect.InvocationTargetException;

import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.dialogs.ProgressMonitorDialog;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.ui.INewWizard;
import org.eclipse.ui.IWorkbench;

import es.capgemini.extjsplugin.core.ExtJSFormBuilder;

/**
 * Clase principal que lanza las cada una de las pantallas del wizard para generar nuevos componentes
 * @author Lisandro Medrano
 *
 */
public class GeneratorWizard extends Wizard implements INewWizard {
	//wizard pages
	//Step1
	private ClassSelectionPage classSelectionPage;
	//Step2
	public AttributesSelectionPage attributesSelectionPage;
	//Step3
	public ComponentSelectionPage componentSelectionPage;
	
	public GeneratorWizardModel model;
	// workbench selection when the wizard was started
	protected IStructuredSelection selection;
	// the workbench instance
	protected IWorkbench workbench;
	
	public GeneratorWizard() {
		super();
		model = new GeneratorWizardModel();
	}
	@Override
	public boolean isHelpAvailable() {
		return false;
	}
	public void addPages()
	{
		classSelectionPage=new ClassSelectionPage("stage1");
		addPage(classSelectionPage);
		attributesSelectionPage=new AttributesSelectionPage("Stage2");
		addPage(attributesSelectionPage);
		componentSelectionPage=new ComponentSelectionPage("Stage3");
		addPage(componentSelectionPage);
//		holidayPage = new HolidayMainPage(workbench, selection);
//		addPage(holidayPage);
//		planePage = new PlanePage("");
//		addPage(planePage);
//		carPage = new CarPage("");
//		addPage(carPage);
	}
	@Override
	public boolean performFinish() {
		componentSelectionPage.saveDataToModel();
		ProgressMonitorDialog dialog = new ProgressMonitorDialog(getShell()); 
		ExtJSFormBuilder builder=new ExtJSFormBuilder(model);

		try {
			dialog.run(true, true, builder);			
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return true;
	}

	@Override
	public void init(IWorkbench workbench, IStructuredSelection selection) {
		this.workbench = workbench;
		this.selection = selection;
	}
	@Override
	public boolean canFinish() {
		
		return componentSelectionPage.canFinish()&&getContainer().getCurrentPage()==componentSelectionPage;
	}

}
